package com.plian.system.service.pm.listedcompany.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.StringUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.sys.file.FileDao;
import com.plian.system.entity.pm.listedcompany.ExpenditureItemDetail;
import com.plian.system.mapper.pm.listedcompany.ExpenditureItemDetailMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pm.listedcompany.IExpenditureItemDetailService;
import com.plian.system.service.sys.file.FileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/2/27 11:08 AM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class ExpenditureItemDetailServiceImpl extends BaseServiceImpl<ExpenditureItemDetailMapper, ExpenditureItemDetail> implements IExpenditureItemDetailService {

    private final FileService fileService;

    private final FileDao fileDao;

    @Override
    public boolean saveOrUpdate(ExpenditureItemDetail expenditureItemDetail) {
        boolean flag = false;
        if (!Optional.ofNullable(expenditureItemDetail.getId()).isPresent()) {
            flag = true;
        }
        boolean result = super.saveOrUpdate(expenditureItemDetail);
        if (flag){
            fileService.saveAll(String.valueOf(expenditureItemDetail.getId()), expenditureItemDetail.getDocList());
        }else {
            fileService.edit(String.valueOf(expenditureItemDetail.getId()), expenditureItemDetail.getDocList());
        }
        if (StringUtil.isNotBlank(expenditureItemDetail.getAttachIds())){
            fileService.upYthFile(expenditureItemDetail.getAttachIds(),String.valueOf(expenditureItemDetail.getId()));
        }
        return result;
    }

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<ExpenditureItemDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ExpenditureItemDetail::getBaseId, ids);
        List<ExpenditureItemDetail> eventDetails = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(eventDetails)){
            return deleteLogic(eventDetails.stream().map(ExpenditureItemDetail::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public void setFiles(List<ExpenditureItemDetail> details){
        List<String> ids = details.stream().map(m -> m.getId() + "").collect(Collectors.toList());
        try {
            List<PageData> files = fileDao.findbyParentIds(ids);
            Map<String, List<PageData>> childMap = files.stream().collect(Collectors.groupingBy(file -> file.getString("parentId")));
            details.forEach(eventDetail ->{
                List<HashMap> hashMaps = new ArrayList<>();
                List<PageData> file = childMap.get(eventDetail.getId()+"");
                if (CollectionUtil.isNotEmpty(file)){
                    file.forEach(m ->{
                        fileService.setViewInfo(m);
                        hashMaps.add(m);
                    });
                }
                eventDetail.setDocList(hashMaps);
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
